package com.gitee.wsl.compose.chart.xy

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.unit.Constraints
import com.gitee.wsl.compose.chart.SelectCheck
import com.gitee.wsl.compose.chart.xy.model.PointEntry
import com.gitee.wsl.compose.ui.container.hoverable.HoverableElementAreaScope

typealias SymbolSlate<P> = @Composable HoverableElementAreaScope.(P,selected: Boolean) -> Unit

@Composable
internal fun <X, Y, P : PointEntry<X, Y>> XYGraphScope<X, Y>.Symbols(
    data: List<P>,
    selectCheck: SelectCheck = { false },
    symbol: SymbolSlate<P>? = null,
) {
    if (symbol != null) {
        Layout(
            modifier = Modifier.fillMaxSize(),
            content = {
                data.indices.forEach {
                    symbol(this, data[it],selectCheck(it))
                }
            }
        ) { measurables: List<Measurable>, constraints: Constraints ->
            val size = Size(constraints.maxWidth.toFloat(), constraints.maxHeight.toFloat())

            layout(constraints.maxWidth, constraints.maxHeight) {
                data.indices.forEach {
                    val p = measurables[it].measure(constraints.copy(minWidth = 0, minHeight = 0))
                    var position = scale(data[it], size)
                    position -= Offset(p.width / 2f, p.height / 2f)
                    p.place(position.x.toInt(), position.y.toInt())
                }
            }
        }
    }
}